package com.micangcaihang.micang.adapter;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.config.PictureMimeType;
import com.micangcaihang.common.loader.ILoader;
import com.micangcaihang.common.loader.LoaderManager;
import com.micangcaihang.common.utils.ALog;
import com.micangcaihang.common.utils.StringUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.entity.TaskAccessoryInfoEntity;

import java.io.File;
import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/8/11
 *     update : 2017/8/11
 *     desc   : 附件信息
 *     version: 1.0
 * </pre>
 */
public class DPictureAdapter extends BaseQuickAdapter<TaskAccessoryInfoEntity.FileContentBean, BaseViewHolder> {
    private int selectMax = 99;
    public static final int TYPE_CAMERA = 1;
    public static final int TYPE_PICTURE = 2;
    private boolean isAddType = true;


    public DPictureAdapter(@LayoutRes int layoutResId, @Nullable List<TaskAccessoryInfoEntity.FileContentBean> data) {
        super(layoutResId, data);
    }

    public DPictureAdapter(@LayoutRes int layoutResId, @Nullable List<TaskAccessoryInfoEntity.FileContentBean> data, boolean isAddType) {
        super(layoutResId, data);
        this.isAddType = isAddType;
    }


    @Override
    protected void convert(BaseViewHolder helper, TaskAccessoryInfoEntity.FileContentBean item) {
        ImageView iv = helper.getView(R.id.iv_img);
        switch (helper.getItemViewType()) {
            case TYPE_CAMERA:
                LoaderManager.getLoader().loadResource(iv, R.drawable.ic_photo_add, null);
                break;
            case TYPE_PICTURE:
                if (PictureMimeType.isAudio(item.getFile_path())) {
                    LoaderManager.getLoader().loadResource(iv, R.drawable.icon_audio, null);
                } else if (!StringUtils.isEmpty(item.getPath())) {
                    LoaderManager.getLoader().loadFile(iv, new File(StringUtils.isEmpty(item.getCompressPath()) ? item.getPath() : item.getCompressPath()), new ILoader.Options(160, 160, 0));
                } else {
                    LoaderManager.getLoader().loadNet(iv, item.getFile_path(), new ILoader.Options(160, 160, 0));
                }
                // 图片
                if (item.isCompressed()) {
                    ALog.i("compress image result:", new File(item.getCompressPath()).length() / 1024 + "k");
                    ALog.i("压缩地址::", item.getCompressPath());
                } else {
                    ALog.i("原图地址::", StringUtils.isEmpty(item.getPath()) ? "网络图片" : item.getPath());
                }
                break;
        }
    }

    private boolean isShowAddItem(int position) {
        if (isAddType) {
            int size = mData.size() == 0 ? 0 : mData.size();
            return position == size;
        } else {
            return false;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PICTURE;
        }
    }

    @Override
    public int getItemCount() {
        if (mData.size() < selectMax && isAddType) {
            return super.getItemCount() + 1;
        } else {
            return super.getItemCount();
        }
    }
}
